﻿Imports System.Data.OleDb
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports AjaxControlToolkit


Public Class TorontoPictureDetail
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        
        If Not IsPostBack Then
            populateList()
            populateCommentList()
            checkLike()
            populateRating()
            populateTags()
            checkTags()
            getTags()
            logVisits()
        End If

        If Not Me.Page.User.IsInRole("Admin") Then
            chk_Active.Visible = False
            btn_Active.Visible = False
            gv_Comments.Columns.Item(0).Visible = False
            gv_Comments.Columns.Item(1).Visible = False
            gv_Comments.Columns.Item(2).Visible = False
        Else
            chk_Active.Visible = True
            btn_Active.Visible = True
            gv_Comments.Columns.Item(0).Visible = True
            gv_Comments.Columns.Item(1).Visible = True
            gv_Comments.Columns.Item(2).Visible = True
        End If

    End Sub

    Protected Sub checkLike()
        Dim userLike As Boolean
        userLike = False

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("dbDusanCS").ConnectionString)
        oleDbConn.Open()

        Dim SqlString As String = "SELECT UserLike FROM UserLikesQuery WHERE UserName = @param1 AND FK_TorontoPictureID = @param2"
        Dim cmdUserID As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmdUserID.CommandType = CommandType.Text
        cmdUserID.Parameters.AddWithValue("@param1", User.Identity.Name)
        cmdUserID.Parameters.AddWithValue("@param2", Request.Params("ID"))

        Dim readerLikes As OleDbDataReader = cmdUserID.ExecuteReader()

        If readerLikes.HasRows Then
            Do While readerLikes.Read()
                userLike = readerLikes.GetBoolean(0)
            Loop
        End If

        If (userLike = -1) Then
            lbl_Like.Visible = True
            btn_Like.Visible = False
        Else
            lbl_Like.Visible = False
            btn_Like.Visible = True
        End If

    End Sub

    Public Function getUserId() As Int32
        Dim userID As Int32

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("dbDusanCS").ConnectionString)
        oleDbConn.Open()

        Dim SqlString As String = "SELECT ID FROM Users WHERE UserName = @param1"
        Dim cmdUserID As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmdUserID.CommandType = CommandType.Text
        cmdUserID.Parameters.AddWithValue("@param1", User.Identity.Name)

        Dim readerUserID As OleDbDataReader = cmdUserID.ExecuteReader()

        If readerUserID.HasRows Then
            Do While readerUserID.Read()
                userID = readerUserID.GetInt32(0)
            Loop
        End If

        Return userID

    End Function

    Protected Sub btnAddNew_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnAddNew.Click

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("dbDusanCS").ConnectionString)
        oleDbConn.Open()

        'First find ID from Users

        Dim userID As Int32
        userID = getUserId()

        Dim commentsID As Int32

        'Insert record inside Comments

        Dim timeNow As String = Date.Now
        timeNow = timeNow.Replace("#", "")

        Dim SqlString = "Insert into Comments(FK_User,CommentTitle,CommentDescription,DatePosted,Active) Values (@param1,@param2,@param3,@param4,True)"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@param1", userID)
        cmd.Parameters.AddWithValue("@param2", tb_CommentTitle.Text)
        cmd.Parameters.AddWithValue("@param3", tb_CommentDescription.Text)
        cmd.Parameters.AddWithValue("@param4", Convert.ToDateTime(timeNow))
        cmd.ExecuteNonQuery()

        'Find CommentsID

        SqlString = "SELECT TOP 1 ID FROM Comments ORDER BY 1 DESC"
        Dim cmdCommentsID As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmdCommentsID.CommandType = CommandType.Text
        cmdCommentsID.Parameters.AddWithValue("@param1", User.Identity.Name)

        Dim readerCommentsID As OleDbDataReader = cmdCommentsID.ExecuteReader()

        If readerCommentsID.HasRows Then
            Do While readerCommentsID.Read()
                commentsID = readerCommentsID.GetInt32(0)
            Loop
        End If

        'Insert record inside TorontoPicturesCommentsJunction

        SqlString = "Insert into TorontoPicturesCommentsJunction(FK_TorontoPictures,FK_Comments) Values (@param1,@param2)"
        Dim cmdT As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmdT.CommandType = CommandType.Text
        cmdT.Parameters.AddWithValue("@param1", Request.Params("ID"))
        cmdT.Parameters.AddWithValue("@param2", commentsID)
        cmdT.ExecuteNonQuery()

        Response.Redirect("ThankYou.aspx?back=TorontoPicturesList.aspx&message=added new comment")


    End Sub
    Protected Sub btn_Like_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_Like.Click

        Dim userID As Int32
        userID = getUserId()

        Dim LikeID As Int32
        LikeID = 0

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("dbDusanCS").ConnectionString)
        oleDbConn.Open()

        Dim SqlString As String = "SELECT ID FROM UserLikes WHERE FK_UserId = @param1 AND FK_TorontoPictureID = @param2"
        Dim cmdUserID As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmdUserID.CommandType = CommandType.Text
        cmdUserID.Parameters.AddWithValue("@param1", userID)
        cmdUserID.Parameters.AddWithValue("@param2", Request.Params("ID"))

        Dim readerUserID As OleDbDataReader = cmdUserID.ExecuteReader()

        If readerUserID.HasRows Then
            Do While readerUserID.Read()
                LikeID = readerUserID.GetInt32(0)
            Loop
        End If

        If LikeID = 0 Then
            SqlString = "Insert into UserLikes(FK_UserId,UserLike,FK_TorontoPictureID) Values (@param1,True,@param2)"
            Dim cmdT As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmdT.CommandType = CommandType.Text
            cmdT.Parameters.AddWithValue("@param1", userID)
            cmdT.Parameters.AddWithValue("@param2", Request.Params("ID"))
            cmdT.ExecuteNonQuery()
        Else
            SqlString = "Update UserLikes SET UserLike = True WHERE FK_UserId = @param1 AND FK_TorontoPictureID = @param2"
            Dim cmdT As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmdT.CommandType = CommandType.Text
            cmdT.Parameters.AddWithValue("@param1", userID)
            cmdT.Parameters.AddWithValue("@param2", Request.Params("ID"))
            cmdT.ExecuteNonQuery()
        End If

        Response.Redirect("ThankYou.aspx?back=TorontoPicturesList.aspx&message=submitted like")

    End Sub

    Protected Sub btn_Active_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_Active.Click

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("dbDusanCS").ConnectionString)
        oleDbConn.Open()

        Dim SqlString As String = "UPDATE TorontoPictures SET Active=@param1 WHERE [ID]= @param2"
        Dim cmdUserID As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmdUserID.CommandType = CommandType.Text
        cmdUserID.Parameters.AddWithValue("@param1", chk_Active.Checked)
        cmdUserID.Parameters.AddWithValue("@param2", Request.QueryString("ID"))

        cmdUserID.ExecuteNonQuery()

        oleDbConn.Close()

        Response.Redirect("ThankYou.aspx?back=TorontoPicturesList.aspx&message=submitted active/noactive status for Picture")

    End Sub

    Protected Sub populateList()

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("dbDusanCS").ConnectionString)
        oleDbConn.Open()

        Dim SqlString As String = "SELECT TOP 1 [PictureOwner], [TorontoPictureName], [Description], [Details], [Location], [YearTaken], [Picture], [Active] FROM [TorontoPictures] WHERE [ID] = @param1"

        If Not Me.Page.User.IsInRole("Admin") Then
            SqlString = SqlString + " AND Active = True"
        End If

        Dim cmdUserID As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmdUserID.CommandType = CommandType.Text
        cmdUserID.Parameters.AddWithValue("@param1", Request.QueryString("ID"))

        Dim readerUserID As OleDbDataReader = cmdUserID.ExecuteReader()

        If readerUserID.HasRows Then
            Do While readerUserID.Read()
                lblPictureOwner.Text = readerUserID.GetString(0)
                lblTorontoPictureName.Text = readerUserID.GetString(1)
                lblDescription.Text = readerUserID.GetString(2)
                lblDetails.Text = readerUserID.GetString(3)
                lblYearTaken.Text = readerUserID.GetString(5)
                imgPicture.ImageUrl = readerUserID.GetString(6)

                If Me.Page.User.IsInRole("Admin") Then
                    chk_Active.Checked = readerUserID.GetBoolean(7)
                End If

            Loop
        End If

        oleDbConn.Close()

    End Sub

    Protected Sub populateCommentList()

        Dim fillList As String = "SELECT [ID], [CommentTitle], [CommentDescription], [UserName], [Active], SWITCH(Active=True,'Deactivate',Active=False,'Activate') AS ButtonText, DatePosted FROM [TorontoPicturesCommentsJunctionQuery] WHERE [FK_TorontoPictures] = " + Request.QueryString("ID")

        If Not Me.Page.User.IsInRole("Admin") Then
            fillList = fillList + " AND Active = True"
        End If

        SqlDataSource1.SelectCommand = fillList
        gv_Comments.DataBind()

    End Sub


    Protected Sub gv_Comments_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gv_Comments.RowCommand

        If (e.CommandName = "ChangeActiveStatus") Then


            ' Retrieve the row index stored in the CommandArgument property.
            Dim index As Integer = Convert.ToInt32(e.CommandArgument)

            ' Retrieve the row that contains the button 
            ' from the Rows collection.
            Dim selectedRow As GridViewRow = gv_Comments.Rows(index)

            Dim idCell As TableCell = selectedRow.Cells(0)
            Dim id As String = idCell.Text

            Dim idCell2 As TableCell = selectedRow.Cells(1)
            Dim id2 As String = idCell2.Text

            Dim active As Boolean

            If id2 = "True" Then
                active = False
            ElseIf id2 = "False" Then
                active = True
            End If


            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("dbDusanCS").ConnectionString)
            oleDbConn.Open()

            Dim SqlString As String = "UPDATE Comments SET Active = @param1  WHERE ID = @param2"

            Dim cmdUserID As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmdUserID.CommandType = CommandType.Text
            cmdUserID.Parameters.AddWithValue("@param1", active)
            cmdUserID.Parameters.AddWithValue("@param2", id)

            cmdUserID.ExecuteReader()

            oleDbConn.Close()

            Response.Redirect("ThankYou.aspx?back=TorontoPicturesList.aspx&message=changed active status for Comment")

        End If

    End Sub

    Protected Sub populateRating()

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("dbDusanCS").ConnectionString)
        oleDbConn.Open()

        Dim SqlString As String = "SELECT IIF( ISNULL(AVG(Rating)), 0 , (AVG(Rating) )), COUNT(Rating) FROM Rating WHERE FK_TorontoPictureID = @param1"

        Dim cmdUserID As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmdUserID.CommandType = CommandType.Text
        cmdUserID.Parameters.AddWithValue("@param1", Request.QueryString("ID"))

        Dim readerUserID As OleDbDataReader = cmdUserID.ExecuteReader()

        Dim averageRanking As String = ""
        Dim ratingCount As String = ""

        If readerUserID.HasRows Then
            Do While readerUserID.Read()
                Rating1.CurrentRating = readerUserID.Item(0)

                averageRanking = readerUserID.Item(0).ToString()
                ratingCount = readerUserID.Item(1).ToString()
            Loop
        End If

        averageRanking = Math.Round(Convert.ToDecimal(averageRanking), 2).ToString()
        lblRatingStatus.Text = String.Format("{0} Users have rated. Average Rating = {1}", ratingCount, averageRanking)

        oleDbConn.Close()

    End Sub

    Protected Sub OnRatingChanged(ByVal sender As Object, ByVal e As RatingEventArgs)

        Dim userID As Int32
        userID = getUserId()
        Dim userIDRating As Int32 = 0

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("dbDusanCS").ConnectionString)
        oleDbConn.Open()

        'First check if user already rated this picture
        Dim SqlString As String = "SELECT ID FROM Rating WHERE FK_UserID = @param1 AND FK_TorontoPictureID = @param2"
        Dim cmdUserID As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmdUserID.CommandType = CommandType.Text

        cmdUserID.Parameters.AddWithValue("@param1", userID)
        cmdUserID.Parameters.AddWithValue("@param2", Request.QueryString("ID"))

        Dim readerUserID As OleDbDataReader = cmdUserID.ExecuteReader()

        If readerUserID.HasRows Then
            Do While readerUserID.Read()
                userIDRating = readerUserID.Item(0)
            Loop
        End If

        If userIDRating > 0 Then
            Dim str_message As String = "You have already rated"
            Response.Write("<script>alert('" & str_message & "')</script>")
            Return
        End If


        ' If doesn't have insert new record
        SqlString = "INSERT INTO Rating (FK_UserID,FK_TorontoPictureID,Rating) VALUES(@param1,@param2,@param3)"
        cmdUserID = New OleDbCommand(SqlString, oleDbConn)
        cmdUserID.CommandType = CommandType.Text

        cmdUserID.Parameters.AddWithValue("@param1", userID)
        cmdUserID.Parameters.AddWithValue("@param2", Request.QueryString("ID"))
        cmdUserID.Parameters.AddWithValue("@param3", e.Value)

        cmdUserID.ExecuteNonQuery()

        oleDbConn.Close()

        Response.Redirect("ThankYou.aspx?back=TorontoPicturesList.aspx&message=submitted rating for Picture")

    End Sub


    Protected Sub btnAddTagsClick(ByVal sender As Object, ByVal e As EventArgs) Handles btnAddTags.Click

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("dbDusanCS").ConnectionString)
        oleDbConn.Open()

        Dim SqlString = ""
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        Dim tagID As Int32
        Dim recordID As Int32 = 0
        Dim readerTag As OleDbDataReader

        Dim userID As Int32
        userID = getUserId()
       
        'Loop to all checkbox list

        For Each item In chkAddTag.Items
            If item.Selected = True Then

                SqlString = "SELECT ID FROM Tags WHERE TagName = @param1"
                cmd = New OleDbCommand(SqlString, oleDbConn)
                cmd.CommandType = CommandType.Text
                cmd.Parameters.AddWithValue("@param1", item.ToString())

                readerTag = cmd.ExecuteReader()

                If readerTag.HasRows Then
                    Do While readerTag.Read()
                        tagID = readerTag.GetInt32(0)
                    Loop
                End If

                recordID = 0

                SqlString = "SELECT ID FROM TorontoPicturesTagsJunction WHERE FK_Tags = @param1 AND FK_TorontoPictures = @param2 AND FK_User = @param3"
                cmd = New OleDbCommand(SqlString, oleDbConn)
                cmd.CommandType = CommandType.Text
                cmd.Parameters.AddWithValue("@param1", tagID)
                cmd.Parameters.AddWithValue("@param2", Request.QueryString("ID"))
                cmd.Parameters.AddWithValue("@param3", userID)

                readerTag = cmd.ExecuteReader()

                If readerTag.HasRows Then
                    Do While readerTag.Read()
                        recordID = readerTag.GetInt32(0)
                    Loop
                End If

                If recordID = 0 Then

                    SqlString = "Insert into TorontoPicturesTagsJunction (FK_TorontoPictures,FK_Tags,FK_User) Values (@param1,@param2,@param3)"
                    cmd = New OleDbCommand(SqlString, oleDbConn)
                    cmd.CommandType = CommandType.Text
                    cmd.Parameters.AddWithValue("@param1", Request.QueryString("ID"))
                    cmd.Parameters.AddWithValue("@param2", tagID.ToString())
                    cmd.Parameters.AddWithValue("@param3", userID)
                    cmd.ExecuteNonQuery()

                End If



            End If
        Next

        oleDbConn.Close()

        Response.Redirect("ThankYou.aspx?back=TorontoPicturesList.aspx&message=submitted tags for Picture")

    End Sub

    Protected Sub btn_AddNewTag_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_AddNewTag.Click

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("dbDusanCS").ConnectionString)
        oleDbConn.Open()

        Dim readerTag As OleDbDataReader
        Dim tagID As Int32 = 0
        Dim recordID As Int32 = 0

        Dim userID As Int32
        userID = getUserId()

        Dim SqlString = "SELECT ID FROM Tags WHERE TagName = @param1"
        Dim cmd = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@param1", txt_NewTag.Text)

        readerTag = cmd.ExecuteReader()

        If readerTag.HasRows Then
            Do While readerTag.Read()
                tagID = readerTag.GetInt32(0)
            Loop
        End If

        If tagID = 0 Then
            'Insert New Tag
            SqlString = "INSERT INTO Tags(TagName) VALUES (@param1)"
            cmd = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text

            cmd.Parameters.AddWithValue("@param1", txt_NewTag.Text)
            cmd.ExecuteReader()

        End If

        SqlString = "SELECT ID FROM Tags WHERE TagName = @param1"
        cmd = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@param1", txt_NewTag.Text)

        readerTag = cmd.ExecuteReader()

        If readerTag.HasRows Then
            Do While readerTag.Read()
                tagID = readerTag.GetInt32(0)
            Loop
        End If



        SqlString = "SELECT ID FROM TorontoPicturesTagsJunction WHERE FK_Tags = @param1 AND FK_TorontoPictures = @param2 AND FK_User = @param3"
        cmd = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@param1", tagID)
        cmd.Parameters.AddWithValue("@param2", Request.QueryString("ID"))
        cmd.Parameters.AddWithValue("@param3", userID)

        readerTag = cmd.ExecuteReader()

        If readerTag.HasRows Then
            Do While readerTag.Read()
                recordID = readerTag.GetInt32(0)
            Loop
        End If

        If recordID = 0 Then

            'Insert new record for Picture
            SqlString = "Insert into TorontoPicturesTagsJunction (FK_TorontoPictures,FK_Tags,FK_User) Values (@param1,@param2,@param3)"
            cmd = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@param1", Request.QueryString("ID"))
            cmd.Parameters.AddWithValue("@param2", tagID)
            cmd.Parameters.AddWithValue("@param3", userID)
            cmd.ExecuteNonQuery()

        End If

        oleDbConn.Close()

        Response.Redirect("ThankYou.aspx?back=TorontoPicturesList.aspx&message=added new tag for Picture")

    End Sub

    Protected Sub populateTags()

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("dbDusanCS").ConnectionString)
        oleDbConn.Open()

        Dim userID As Int32
        userID = getUserId()


        Dim SqlString As String = "SELECT [ID], [TagName] FROM [Tags] ORDER BY [TagName]"
        Dim SqlStringCheck As String = ""

        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        Dim cmdCheck As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text

        Dim readerTag As OleDbDataReader = cmd.ExecuteReader()

        chkAddTag.DataSource = readerTag
        chkAddTag.DataBind()

        oleDbConn.Close()


    End Sub


    Protected Sub checkTags()

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("dbDusanCS").ConnectionString)
        oleDbConn.Open()

        Dim SqlString As String = ""
        Dim cmdCheck As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        Dim readerCheck As OleDbDataReader

        Dim userID As Int32
        userID = getUserId()

        For Each li As ListItem In chkAddTag.Items

            SqlString = "SELECT ID FROM TorontoPicturesTagsJunction WHERE FK_Tags = @param1 AND FK_TorontoPictures = @param2 AND FK_User = @param3 "
            cmdCheck = New OleDbCommand(SqlString, oleDbConn)
            cmdCheck.CommandType = CommandType.Text
            cmdCheck.Parameters.AddWithValue("@param1", li.Value)
            cmdCheck.Parameters.AddWithValue("@param2", Request.QueryString("ID"))
            cmdCheck.Parameters.AddWithValue("@param3", userID)

            readerCheck = cmdCheck.ExecuteReader()

            If readerCheck.HasRows Then

                Do While readerCheck.Read()

                    li.Selected = True
                    li.Enabled = False

                Loop

            End If


        Next

        oleDbConn.Close()

    End Sub

    Protected Sub getTags()

        Dim userID As Int32
        userID = getUserId()

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("dbDusanCS").ConnectionString)
        oleDbConn.Open()

        Dim TagText As String = "Tagged: "

        Dim SqlString As String = ""
        Dim cmdCheck As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        Dim readerCheck As OleDbDataReader

        SqlString = "SELECT TagName, COUNT(*) FROM TagUserQuery WHERE FK_TorontoPictures = @param1 GROUP BY TagName "
        cmdCheck = New OleDbCommand(SqlString, oleDbConn)
        cmdCheck.CommandType = CommandType.Text
        cmdCheck.Parameters.AddWithValue("@param1", Request.QueryString("ID"))

        readerCheck = cmdCheck.ExecuteReader()

        If readerCheck.HasRows Then

            Do While readerCheck.Read()

                If TagText = "" Then
                    TagText = readerCheck.Item(0).ToString() + " ( x " + readerCheck.Item(1).ToString() + " ), "
                Else
                    TagText = TagText + readerCheck.Item(0).ToString() + " ( x " + readerCheck.Item(1).ToString() + " ), "
                End If

            Loop

        End If

        lblTags.Text = TagText

        oleDbConn.Close()

    End Sub

    Protected Sub logVisits()

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("dbDusanCS").ConnectionString)
        oleDbConn.Open()

        Dim SqlString = ""
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        Dim recordID As Int32 = 0
        Dim readerTag As OleDbDataReader

        Dim userID As Int32
        userID = getUserId()

        Dim timeNow As String = Date.Now
        timeNow = timeNow.Replace("#", "")

        SqlString = "SELECT ID FROM UserVisit WHERE FK_TorontoPicture = @param1 AND FK_User = @param2"
        cmd = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@param1", Request.QueryString("ID"))
        cmd.Parameters.AddWithValue("@param2", userID)

        readerTag = cmd.ExecuteReader()

        If readerTag.HasRows Then
            Do While readerTag.Read()
                recordID = readerTag.GetInt32(0)
            Loop
        End If


        If recordID = 0 Then

            SqlString = "Insert into UserVisit (FK_TorontoPicture,FK_User,VisitDateTime) Values (@param1,@param2,@param3)"
            cmd = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@param1", Request.QueryString("ID"))
            cmd.Parameters.AddWithValue("@param2", userID)
            cmd.Parameters.AddWithValue("@param3", timeNow)
            cmd.ExecuteNonQuery()

        End If

        oleDbConn.Close()

    End Sub

    Public Function getLatLng() As String

        Dim LatLngString As String = ""


        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("dbDusanCS").ConnectionString)
        oleDbConn.Open()

        Dim SqlString As String = "SELECT LatLag FROM TorontoPictures WHERE ID = @param1"

        Dim cmdUserID As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmdUserID.CommandType = CommandType.Text
        cmdUserID.Parameters.AddWithValue("@param1", Request.QueryString("ID"))

        Dim readerUserID As OleDbDataReader = cmdUserID.ExecuteReader()



        If readerUserID.HasRows Then
            Do While readerUserID.Read()
                LatLngString = "new google.maps.LatLng(" + readerUserID.Item(0) + ")"
            Loop
        End If

        Return LatLngString

    End Function

End Class